//
//  PendingPaymentViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/2.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//
// 待付款详情

import UIKit

class PendingPaymentViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource, BottomShadow, AddCornerUIRectCorner {
    let cellHeight: CGFloat = 103
    let leftMargin: CGFloat = 18
    let headerViewHeight: CGFloat = 40
    @IBOutlet var shadowViews: [UIView]!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var needPayLabel: UILabel!
    @IBOutlet weak var userNameLabel: UILabel!
    @IBOutlet weak var phoneLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var mainTableView: UITableView!
    @IBOutlet weak var mainTableViewHeight: NSLayoutConstraint!
    @IBOutlet weak var orderNumberLabel: UILabel!
    @IBOutlet weak var orderTimeLabel: UILabel!
    @IBOutlet weak var invoiceTypeLabel: UILabel!
    @IBOutlet weak var totalPriceLabel: UILabel!
    @IBOutlet weak var freightPriceLabel: UILabel!
    @IBOutlet weak var finalPriceLabel: UILabel!
    @IBOutlet weak var payButton: UIButton!
    @IBOutlet weak var cancelButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        var zPositionValue: CGFloat = 1
        let _ = shadowViews.map({setShadow(view: $0, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
            $0.layer.zPosition = zPositionValue
            zPositionValue -= 0.1})
        let buttons = [payButton, cancelButton]
        
        for btn in buttons {
            if let button = btn {
                button.layer.cornerRadius = button.height / 2
                button.layer.borderColor = button.currentTitleColor.cgColor
                button.layer.borderWidth = 1
            }
        }
        
        mainTableView.register(UINib(nibName: "\(PendingPaymentTableViewCell.self)", bundle: nil), forCellReuseIdentifier: "\(PendingPaymentTableViewCell.self)")
        setData()
    }
    
    func setData() {
        needPayLabel.attributedText = NSMutableAttributedString.differentColorAndSizeString(first: (firstString: "需付款", firstColor: ONColor.orderNormalTextColor, firstSize: 10), second: (secondString: "￥345", secondColor: ONColor.currencyRedColor, secondSize: 11))
        addressLabel.text = "坑就是打卡机刷的卡坚实的看见爱上大卡司觉得哈坑就是德哈卡觉得哈"
        addressLabel.sizeToFit()
        mainTableView.tableHeaderView = setHeaderView()
        mainTableViewHeight.constant = headerViewHeight + cellHeight * 2
        
    }
    
    // MARK: - TableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "\(PendingPaymentTableViewCell.self)") as? PendingPaymentTableViewCell
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeight
    }
    
    @IBAction func payTap(_ sender: UIButton) {
    }
    
    @IBAction func cancelTap(_ sender: UIButton) {
        CancelOrderView().showData { title in
            
        }
    }
    
    // MARK: - 设置头
    func setHeaderView() -> UIView {
        let headerView = UIView()
        headerView.backgroundColor = UIColor.white
        headerView.frame = CGRect(x: 0, y: 0, width: mainTableView.width, height: headerViewHeight)
        
        let orderNumberLabel = UILabel()
        orderNumberLabel.text = "订单编号：\("43748238476")"
        orderNumberLabel.textColor = ONColor.orderNormalTextColor
        orderNumberLabel.font = UIFont.systemFont(ofSize: 11)
        orderNumberLabel.sizeToFit()
        orderNumberLabel.frame.origin = CGPoint(x: leftMargin, y: headerView.height / 2 - orderNumberLabel.height / 2)
        headerView.addSubview(orderNumberLabel)
        
        let lineView = UIView()
        lineView.backgroundColor = ONColor.cutLineColor
        lineView.frame = CGRect(x: leftMargin, y: headerView.height - 1, width: headerView.width - leftMargin * 2, height: 1)
        headerView.addSubview(lineView)
        return headerView
    }

}
